function callAjaxToApplyPaymentReceivable(invId, isPayable, paymentMode, paymentRef, amount, receivedDate,balance,jobId ){
	var flag =false;
	var numbererrorstr='<div class="ketchup-errorPar"><div class="ketchup-error"><ul><li>Enter Numeric Value.</li></ul><span></span></div></div>'
	partiallyPaid = false;
	if(parseFloat(balance) > parseFloat(amount))
		partiallyPaid = true;

	var due;
	if(amount !="")
		due = balance - amount;
		
	if(isNaN(amount))
	{
		flag =true;
	}
	else if(amount <= 0)
	{
		flag =true;
	}
	
	if(flag)
	{
	$('.numberval').before(numbererrorstr);
	
		return;
	}

	if(!flag){
	$('.numberval').parent().find(".ketchup-errorPar").remove();
	var paymentDtls ={     
		"invId":invId,
		"isPayable":isPayable,
		"paymentMode": paymentMode, 
		"paymentRef":paymentRef, 
		"amount":amount, 
		"receivedDate":receivedDate,
		"paidStatus":partiallyPaid,
		"due":due,
		"jobId":jobId,
		"type":"receivable"
	};
	flag = $(".mandatoryfield").RateTableCustomValidator("required",true);
	if(!flag)
	{

	
		return;
	}

	 $.ajaxSetup({
		   jsonp: null,
		   jsonpCallback: null
		});

	    $.ajax({
		  type:'POST',
		  url: 'receivable-invoice.action?applyPaymentReceivable',//name of action?name of method in action
		  dataType: 'json',
		  data: {payment:JSON.stringify(paymentDtls) },
			 
		  success: function(result){			  
			  window.location.reload()   
		  },
		  error:function() { 
			  
		  }
	});
}
}
function callAjaxToStatusChangeForInvoice(invoiceId,jobId,statusId)
{
	var invoiceDetails ={     
		"invoiceId"	:	invoiceId,
		"jobId"		:	jobId,
		"type"		:   "receivable",
		"status"	:	statusId
	};
	 $.ajaxSetup({
		   jsonp	: null,
		   jsonpCallback: null
		});

	$.ajax({
		  type		: 'POST',
		  url		: 'receivable-invoice.action?statusChangeForInvoice',
		  dataType	: 'json',
		  data		: {statusDetails:JSON.stringify(invoiceDetails)},
			 
		  success	: function(result){
			  
			  if(statusId  == 2)
				  alert("Your invoice posted successfully.");
			  else if(statusId  == 5)
				  alert("Your invoice voided successfully.");
			  else if(statusId  == 6)
			     alert("Your invoice voided and new invoice created successfully.");
			 // window.location.reload();
			  
				/*
				var jobId1;
		     var userType;
		     var invoiceId1 ;
				
				$.each(result,function(key,value){
					
					if(key == "userType"){
						
						if(value == 1)
							userType = "receivable";
						else if (value == 2)
							userType ="scopist";
						else if (value == 3)
							userType ="proofer";
						
					}	
					if(key == "JobId")
						jobId1 = value;
					
					if(key == "invoiceId")							
						invoiceId1  = value;
					
				});*/
			  
			  /*alert(jobId);
			  alert(invoiceId);*/
			  if(statusId  == 5 || statusId  == 6)
				 window.location.href="billing-receivable.action";
			  else 
			     window.location.href="receivable-invoice.action?viewAgentInvoice=&jobId="+jobId+"&invoiceId="+invoiceId;
		  },
		  error:function() { 
		}
	});
}
$.fn.RateTableCustomValidator=function(validatestring,submit,form){
	var flag = false;
var flagInc = 0;
var Requirederrorstr='<div class="ketchup-errorPar"><div class="ketchup-error"><ul><li>This Field is Required.</li></ul><span></span></div></div>'

if(submit){

	$(this).each(function(i){

   flag=requiredfieldvalidtion($(this).val());	
   
if(!flag){$(this).parent().find(".ketchup-errorPar").remove();$(this).before(Requirederrorstr);
flagInc++;$(this).focus();}
else {$(this).parent().find(".ketchup-errorPar").remove();

}
});

if(flagInc > 0)
	retflag = false;
else 
retflag = true;

}  
$(this).live("blur",function(){
retflag=requiredfieldvalidtion($(this).val());
if (!retflag){$(this).parent().find(".ketchup-errorPar").remove();$(this).before(Requirederrorstr);}
else{$(this).parent().find(".ketchup-errorPar").remove();}
});

return retflag;
}; 
$(document).ready(function(){
$('.invoiceestimatePages').live("change",function(){

//var jobId= $(this).closest(".job-row").find('.jobId').val();

$("#estimatePages-form").dialog({
height: 350,
width: 600,
modal: true,
title:"MY HOURS TO PERFORM TASK",
hide: 'fold',
show: 'blind',
buttons: {

"Save":function(){
saveAdminstrativeHoursInJob(actualUserId,jobId);
},
"Cancel": function() {
$('.invoiceestimatePages').closest("div").next().find("input:visible").focus();
$(this).dialog('close');
}
},

});
$('.ui-icon').click(function(){
		
$('.invoiceestimatePages').closest("div").next().find("input:visible").focus();
});
callAjaxToDisplayAdminstrativeHours(actualUserId,jobId);
});
	$(".mandatoryfield").RateTableCustomValidator("required",false);
	
	$(".numberval").live("blur",function(){
		values=($(this).val()).split('.');
		one=values[0];
		two=values[1];
		
		var num = one;
		var one1 = num.toString();
		one1 = one1.replace(/^0+/,'');
		
		if(one=='' && two == '' ){ 
			
			var num=0;
			$(this).val(num.toFixed(2));
			return false;
		}
		
		var value1 = $.trim($(this).val());
		if(value1=='' || value1 == 0 ){ 
			
			var num=0;
			$(this).val(num.toFixed(2));
			return false;
		}
		
		var precisionflag=isNaN(one)?false:true;
		var scaleflag=isNaN(two)?false:true;
				
		if(!precisionflag){ 
			var num=0;
			$(this).val(num.toFixed(2));
		}
		else if(!scaleflag){
			var num=parseInt(one1,10).toFixed(2);
			$(this).val(num);
		}
		else if(values.length>1){
			if(!scaleflag && precisionflag)
			{
			var num=one;
			$(this).val(num.toFixed(2));
			}
			else if(scaleflag){
		   	var numaddO = one1+"."+two;
			var num=roundNumber(numaddO,2);
			
			if(!isNaN(num.toString().split(".")[1]))
			{
			var numaddO =  num.toString().split(".")[1].length==1?one1+"."+num.toString().split(".")[1]+"0":num;
			//alert(numaddO);
			$(this).val(numaddO);
			}
			else{
			var num=parseInt(numaddO,10).toFixed(2);
			$(this).val(num);
			}
			}
			
		}
				
		
	});
	function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
	}
	
});
function requiredfieldvalidtion(eleVal){
	var flag=false;
	if(eleVal=="")
		flag=false;
	else
		flag=true;
		
	return flag;
	}
function decimalNumbervalidtion(eleVal){
			var flag=true;
			if(isNaN(eleVal))
				flag =false;
			return flag;
		}
	function saveAdminstrativeHoursInJob(actualUserId,jobId){
	var str ='';	 
	var flag = true;
	var errmsg = $('#errorDiv');
	var skillAdminHrs = $('#skillAdminHrsId').val();
	//var numbererrorstr='<div class="ketchup-errorPar"><div class="ketchup-error"><ul><li>Enter Numeric Value.</li></ul></div></div>';
	var defaultSkillType = $('input[name=hrstodoname]:radio:checked').val();
	var flag= decimalNumbervalidtion(skillAdminHrs);
	if(!flag)
	{
		//$('.decimalnumberVal').before(numbererrorstr);
		$('#errorDiv').text("Enter Numeric Value.").addClass( "ui-state-highlight").css({"background":"#FFE4DD", "margin-bottom": "5px", "width": "250px", "padding": "3px", "font-family": "verdana","font-weight": "bold","color":"#000"});
    
		
		return;
	}
	if(flag)
		  {
$("#errorDiv").empty();
			$("#errorDiv").removeClass("errdiv");
 $.ajaxSetup({
		   jsonp: null,
		   jsonpCallback: null
		});

	    $.ajax({
		  type:'POST',
		  url: 'qgl-log-pending.action?saveAdminstrativeHoursInJob',
		  dataType: 'json',
		  data: {userId:actualUserId, jobId:jobId,skillAdminHrs:skillAdminHrs,defaultSkillType:defaultSkillType},
			 
		  success: function(result){
			  
					$('#errorDiv').text("Administrative Hours saved Successfully").addClass("alert alert-success");
			$('.invoiceestimatePages').closest("div").next().find("input:visible").focus();
					setTimeout("closewindow()",2000);
				  caluculateAllHours(jobId);
			          
		  },
		  error:function() { 
			  
		  }
 	});	
		  }
}

	function closewindow()
	{
		$("#estimatePages-form").dialog("destroy");
	}

function callAjaxToDisplayAdminstrativeHours(actualUserId,jobId){	
	
	 $.ajaxSetup({
		   jsonp: null,
		   jsonpCallback: null
		});

	    $.ajax({
		  type:'POST',
		  url: 'qgl-log-pending.action?checkIfHoursToPerFormExist',
		  dataType: 'json',
		  data: {userId:actualUserId, jobId:jobId},
			 
		  success: function(result){
			  if(result!=null){
				 //showError(userType, agency);
				//quiggleJobAutoComplete(result, '.est-id',"#estimatePages-form");
				showDialogue(result);
			  }            
		  },
		  error:function() { 
			  
		  }
 	});	
	
}

function showDialogue(result)
{
	
	var str='';
	str +='<div id="errorDiv"></div>';
	 str+=' <fieldset>'
     str+=' <div style="font-size: 14px;font-family: verdana;">';
     str+='<input type="hidden" name="block-id" class="block-id" value=""/>';
     for(var i=0;i<result.length;i++)
	{ 
		 if(result[i].defaultSkillType == 1)
		{
			 str +='<div id="regularDefaultDiv" class="selectedTask">';
    str+='<input type="radio" name="hrstodoname" class="hrstodoname"  id="regularDefault" value="1" class="initial text ui-widget-content ui-corner-all popup" style="margin-left:4px;" checked="true"/>';
		}
		else
		{
			 str +='<div id="regularDefaultDiv">';
			str+='<input type="radio" name="hrstodoname" class="hrstodoname"  id="regularDefault" value="1" class="initial text ui-widget-content ui-corner-all popup" style="margin-left:4px;"/>';
		}

	str+=' <span class="label">Regular</span>';
	str +='<div style="display: inline-table;margin-left: 10px;width: 100px;">';
	str +='<span>Scope</span>';
	str+='<span style="padding-left:5px;">'+result[i].regularScopeHours+'</span>';
	str +='</div>';
	str +='<div style="display: inline-table;margin-left: 10px;width: 100px;">';
	str +='<span>Proof</span>';
	str+='<span style="padding-left:5px;">'+result[i].regularProofHours+'</span>';
	str +='</div>';
	str +='</div>';
	str +='<br>';
	 if(result[i].defaultSkillType == 2)
		{
		  str +='<div id="denseDefaultDiv" class="selectedTask">';
		 str+=' <input type="radio" name="hrstodoname" class="hrstodoname"  id="denseDefault" value="2" class="initial text ui-widget-content ui-corner-all popup" style="margin-left:4px;" checked="true"/>';
		}
		else
		{
			 str +='<div id="denseDefaultDiv">';
str+=' <input type="radio" name="hrstodoname" class="hrstodoname"  id="denseDefault" value="2" class="initial text ui-widget-content ui-corner-all popup" style="margin-left:4px;"/>';
		}
	str+=' <span class="label">Dense</span>';
	str +='<div style="display: inline-table;margin-left: 10px;width: 100px;">';
	str +='<span>Scope</span>';
	str+='<span style="padding-left:5px;">'+result[i].denseScopeHours+'</span>';
	str +='</div>';
	str +='<div style="display: inline-table;margin-left: 10px;width: 100px;">'
	str +='<span>Proof</span>';
	str+='<span style="padding-left:5px;">'+result[i].denseProofHours+'</span>';
	str +='</div>';
	str +='</div>';
	str +='<br>';
	
	 if(result[i].defaultSkillType == 3)
		{
		str +='<div id="easyDefaultDiv" class="selectedTask">';
    str+=' <input type="radio" name="hrstodoname" class="hrstodoname"  id="easyDefault" value="3" class="initial text ui-widget-content ui-corner-all" style="margin-left:4px;" checked="true"/>';
	 
		}
		else
		{
			 str +='<div id="easyDefaultDiv">';
	str+=' <input type="radio" name="hrstodoname" class="hrstodoname"  id="easyDefault" value="3" class="initial text ui-widget-content ui-corner-all popup" style="margin-left:4px;"/>';
		}
	str+=' <span class="label">Easy</span>';
	str +='<div style="display: inline-table;margin-left: 10px;width: 100px;">'
	str +='<span>Scope</span>';
	str+='<span style="padding-left:5px;">'+result[i].easyScopeHours+'</span>';
	str +='</div>';
	str +='<div style="display: inline-table;margin-left: 10px;width: 100px;">'
	str +='<span>Proof</span>';
	str+='<span style="padding-left:5px;">'+result[i].easyProofHours+'</span>';
	str +='</div>';
	str +='</div>';
	str +='<br><br>';
	str +='<div style="font-size:11px;">(To change the values of default fields above, please do so from your Profile page.)</div><br>';
	str +='<div style="font-size:12px;">';
	str+=' <label for="skillAdminHrs">FOR THIS PARTICULAR JOB: Administrative Hours</label>';
	str+='<input type="text" maxlength="10" id="skillAdminHrsId" value="'+result[i].administrativeHours+'" name="skillAdminHr" style="width:60px;height:18px;border-radius: 3px 3px 3px 3px;border:1px solid #ccc;margin-left:1px;padding:3px;display: inline-table;" class="decimalnumberVal skillAdminHrs"/>';
	str +='</div>';
	}
	str+='</div>';
    str+=' </fieldset>';
	$("#estimatePages-formInnerDiv").html(str);
      
}

